﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using LHS.Core.TableInfo;
using MS.Core.Base.Transaction;
using System.Collections.Specialized;

namespace LHS.Win
{
    public partial class frmXoaHD : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmXoaHD()
        {
            this.InitializeComponent();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.DataSource != null)
            {
                if (this.txtReason.Text == "")
                {
                    MessageBox.Show("Vui lòng nhập lý do hủy!", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    this.dataGrid1.Select(this.dataGrid1.CurrentRowIndex);
                    string str = dataGrid1[this.dataGrid1.CurrentRowIndex, 3].ToString(); //patname
                    string str2 = dataGrid1[this.dataGrid1.CurrentRowIndex, 0].ToString();//CLRISN
                    string str3 = dataGrid1[this.dataGrid1.CurrentRowIndex, 1].ToString();//INVISN
                    if (MessageBox.Show("Bạn có muốn hủy hóa đơn có một bệnh án " + str2 + " của bệnh nhân " + str + " này không?", "Hủy hóa Đơn", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        string isn = Class.isn; //user isn dang nhap he thong
                        TransactionManager trans = TransactionManager.ThreadTransactionMgr();
                        try
                        {
                            trans.BeginTransaction();

                            DataTable dtUserUpdatedInvoice = InfoUserUpdatedInvoices.GetStructure();
                            DataRow dr = dtUserUpdatedInvoice.NewRow();
                            dr[InfoUserUpdatedInvoices.CLRISN] = str2;
                            dr[InfoUserUpdatedInvoices.INVISN] = str3;
                            dr[InfoUserUpdatedInvoices.USNREASON] = this.txtReason.Text;
                            dr[InfoUserUpdatedInvoices.USNDATE] = DateTime.Now;
                            dr[InfoUserUpdatedInvoices.USNMONEYBEFOREUPDATE ] = 0;
                            dr[InfoUserUpdatedInvoices.USNSTATUS] = 2;
                            dr[InfoUserUpdatedInvoices.USRISN] = isn;
                            dtUserUpdatedInvoice.Rows.Add(dr);
                            objController.InsertRow(dr, new string[] { InfoUserUpdatedInvoices.USNISN });
                          
                            //xoa hoa don
                            ListDictionary param = new ListDictionary();
                            param.Add(InfoClinicalRecord.CLRISN, str2);
                            objController.Exec_StoreProc("sp_Invoices_Delete",param);
                            
                            trans.CommitTransaction();
                            MessageBox.Show("Đã hủy hóa đơn có một bệnh án :" + str2 + " !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.txtReason.Text = "";
                        }
                        catch (Exception ex)
                        {
                            trans.RollbackTransaction();
                            Class.ShowException(ex);
                        }
                        this.frmXoaHD_Load(sender, e);
                    }
                }
            }

        }

        private void frmXoaHD_Load(object sender, EventArgs e)
        {
            DataTable dt = objController.GetData("vw_FullPayment");

            if (dt == null || dt.Rows.Count == 0)
            {
                base.Close();
            }
            else
            {
                this.dataGrid1.Enabled = true;
                bindingSource1.DataSource = dt;
                this.dataGrid1.DataSource = bindingSource1;
                this.dataGrid1.ReadOnly = true;
                this.dataGrid1.TableStyles.Clear();
                this.loadDataGrid();
            }
        }

        public void loadDataGrid()
        {
            DataGridTableStyle table = new DataGridTableStyle();
            table.MappingName = "vw_FullPayment";
            table.AlternatingBackColor = Color.AliceBlue;
            table.HeaderForeColor = Color.Maroon;
            table.GridLineColor = Color.CornflowerBlue;

            DataGridTextBoxColumn column = new DataGridTextBoxColumn();//0
            column.MappingName = InfoClinicalRecord.CLRISN;
            column.HeaderText = "";
            column.Width = 0;
            table.GridColumnStyles.Add(column);

            DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();//1
            column2.MappingName = InfoInvoices.INVISN;
            column2.HeaderText = "";
            column2.Width = 0;
            table.GridColumnStyles.Add(column2);

            DataGridTextBoxColumn column3 = new DataGridTextBoxColumn();//2
            column3.MappingName = InfoPatients.PATID;
            column3.HeaderText = "Số ID";
            column3.Width = 110;
            table.GridColumnStyles.Add(column3);

            DataGridTextBoxColumn column4 = new DataGridTextBoxColumn();//3
            column4.MappingName = InfoPatients.PATNAME;
            column4.HeaderText = "Tên Bệnh Nhân";
            column4.Width = 210;
            table.GridColumnStyles.Add(column4);

            DataGridTextBoxColumn column5 = new DataGridTextBoxColumn();//4
            column5.MappingName = InfoPatients.PATBIRTHDATE;
            column5.HeaderText = "Ngày Sinh";
            column5.Width = 100;
            table.GridColumnStyles.Add(column5);

            DataGridTextBoxColumn column6 = new DataGridTextBoxColumn();//5
            column6.MappingName = InfoClinicalRecord.CLREXAMINATIONDATE;
            column6.HeaderText = "Ngày Lập Bệnh Án";
            column6.Width = 0x83;
            column6.NullText = string.Empty;
            table.GridColumnStyles.Add(column6);

            DataGridTextBoxColumn column7 = new DataGridTextBoxColumn();//6
            column7.MappingName = InfoClinicalRecord.CLREXAMINATIONTIMES;
            column7.HeaderText = "Lần Khám";
            column7.Width = 0x83;
            column7.NullText = string.Empty;
            table.GridColumnStyles.Add(column7);

            DataGridTextBoxColumn column8 = new DataGridTextBoxColumn();//7
            column8.MappingName = InfoInvoices.INVEXAMINATIONPAYMENT;
            column8.HeaderText = "Tiền Khám";
            column8.Width = 0x83;
            column8.NullText = string.Empty;
            table.GridColumnStyles.Add(column8);

            DataGridTextBoxColumn column9 = new DataGridTextBoxColumn();//8
            column9.MappingName = InfoInvoices.INVMEDICINEPAYMENT;
            column9.HeaderText = "Tiền Thuốc";
            column9.Width = 0x83;
            column9.NullText = string.Empty;
            table.GridColumnStyles.Add(column9);

            DataGridTextBoxColumn column10 = new DataGridTextBoxColumn();//9
            column10.MappingName = InfoInvoices.INVMEDICINESKILLPAYMENT;
            column10.HeaderText = "Tiền Thủ Thuật";
            column10.Width = 0x83;
            column10.NullText = string.Empty;
            table.GridColumnStyles.Add(column10);

            DataGridTextBoxColumn column11 = new DataGridTextBoxColumn();//10
            column11.MappingName = InfoInvoices.INVSURGERYPAYMENT;
            column11.HeaderText = "Tiền Phẫu Thuật";
            column11.Width = 0x83;
            column11.NullText = string.Empty;
            table.GridColumnStyles.Add(column11);
            table.AllowSorting = false;
            this.dataGrid1.TableStyles.Add(table);
        }
    }
}